package model

import (
	"../global"
	"github.com/go-pg/pg"
)

type UserModel struct{}

//创建新用户
func (*UserModel) RegUser(query *User) (err error) {
	_, err = global.DB.Model(query).Insert()
	return
}

//获取用户
func (*UserModel) GetUser(query *User, column []string, listParams *ListParams) (result []User, err error) {
	model := global.DB.Model(&result)
	for k := range column {
		model.Column(column[k])
	}
	if query.ID != 0 {
		model.Where("id=?", query.ID)
	}
	if query.Email != "" {
		model.Where("email=?", query.Email)
	}
	if query.AgentID != 0 {
		model.Where("agent_id=?", query.AgentID)
	}
	if query.ParentID != 0 {
		model.Where("parent_id=?", query.ParentID)
	}
	if query.FullName != "" {
		model.Where("fullname=?", query.FullName)
	}
	if query.NickName != "" {
		model.Where("nickname=?", query.NickName)
	}
	if query.FullName != "" {
		model.Where("fullname=?", query.FullName)
	}
	if query.Mobile != "" {
		model.Where("mobile=?", query.Mobile)
	}
	if query.BankNumber != "" {
		model.Where("bank_number=?", query.BankNumber)
	}
	if query.WXOpenID != "" {
		model.Where("wx_openid=?", query.WXOpenID)
	}
	if query.WXAppID != "" {
		model.Where("wx_appid=?", query.WXAppID)
	}
	if query.IDCardNumber != "" {
		model.Where("idcard_number", query.IDCardNumber)
	}

	if query.SearchTime1 != 0 && query.SearchTime2 == 0 {
		model.Where("reg_time>=?", query.SearchTime1)
	}
	if query.SearchTime1 == 0 && query.SearchTime2 != 0 {
		model.Where("reg_time<=?", query.SearchTime2)
	}
	if query.SearchTime1 != 0 && query.SearchTime2 != 0 && query.SearchTime1 < query.SearchTime2 {
		model.Where("reg_time BETWEEN ? AND ?", query.SearchTime1, query.SearchTime2)
	}
	model.Order("reg_time DESC").Limit(listParams.Limit)
	if listParams.Limit != 0 && listParams.Page != 0 {
		err = model.Offset((listParams.Page - 1) * listParams.Limit).Select()
	}
	err = model.Select()
	return
}

//编辑用户
func (*UserModel) EditUser(query *User) (err error) {
	model := global.DB.Model(&User{})
	model.Where("id=?", query.ID)

	if query.Email != "" {
		model.Set("email=?", query.Email)
	}
	if query.NickName != "" {
		model.Set("nickname=?", query.NickName)
	}
	if query.FullName != "" {
		model.Set("fullname=?", query.FullName)
	}
	if query.BankNumber != "" {
		model.Set("bank_number=?", query.BankNumber)
	}
	if query.RegSource != "" {
		model.Set("reg_source=?", query.RegSource)
	}
	if query.Mobile != "" {
		model.Set("mobile=?", query.Mobile)
	}
	if query.IDCardNumber != "" {
		model.Set("idcard_number=?", query.IDCardNumber)
	}
	if query.Balance != 0 {
		model.Set("balance=?", query.Balance)
	}
	if query.Deleted == true {
		model.Set("deleted=?", true)
	}
	if query.Deleted == false {
		model.Set("deleted=?", false)
	}
	if query.Brokerage != 0 {
		model.Set("brokerage=?", query.Brokerage)
	}
	_, err = model.Update()
	return

}

//删除用户
func (*UserModel) DelUser(IDs []int64) (err error) {
	if len(IDs) > 1 {
		_, err = global.DB.Model(&User{}).WhereIn("id in (?)", pg.In(IDs)).Delete()
	} else {
		_, err = global.DB.Model(&User{}).Where("id=?", IDs[0]).Delete()
	}
	return
}
